package com.zy.web.demo.webservice;

import com.zy.web.demo.webservice.his.ArrayOfString;
import com.zy.web.demo.webservice.his.IpAddressSearchWebServiceSoap;
import com.zy.web.demo.webservice.his.Webservice;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import java.io.IOException;
import java.io.StringWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author <a href="mailto:zhouyong@cqyyt.com">zy</a>
 * @version 1.0
 * @date 2020/11/9 11:49
 */
public class test {


    public static void main(String[] args) throws MalformedURLException {

        //在这里输入接口链接。
        URL url=new URL("http://www.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx?wsdl");
        //Qname里面就需要填写我们一开始找的2个东西。第一个输入namespaceurl。第二个输入service名称。
        QName qName=new QName("http://WebXml.com.cn/","IpAddressSearchWebService");
        //这里直接创建连接。
        Service  service=Service.create(url,qName);
        //调用这个service里面的方法。
        IpAddressSearchWebServiceSoap port = service.getPort(IpAddressSearchWebServiceSoap.class);
        //返回的是ArrayOfString。但是我们可以直接toString转成list。调用方法括号内填入你要查询的iP地址
        ArrayOfString countryCityByIp = port.getCountryCityByIp("123.2.2.3");
        //转化 + 遍历
        List<String> list = countryCityByIp.getString();
        Object[] object = list.toArray();
        for (Object o : object) {
            System.out.println(o);
        }
    }


//    /**
//     * 组装xml参数
//     *
//     * @param map
//     * @param msgType
//     * @return
//     * @throws IOException
//     */
//    public static String getParamXml(Map<String, String> map) throws IOException {
//        Document document = DocumentHelper.createDocument();
//        Element root = DocumentHelper.createElement("items");
//        document.setRootElement(root);
//        for (String key : map.keySet()) {
//            Element element = root.addElement(key);
//            element.setText(map.get(key) == null ? "" : map.get(key));
//        }
//        OutputFormat format = OutputFormat.createCompactFormat();
//        format.setSuppressDeclaration(true);
//        format.setEncoding("UTF-8");
//        StringWriter writer = new StringWriter();
//        XMLWriter output = new XMLWriter(writer, format);
//        output.write(document);
//        writer.close();
//        output.close();
//        return writer.toString();
//    }
//
//    public static void main(String[] args) throws IOException {
////        Map<String, String> param = new HashMap<>(3);
////        param.put("zy","1");
////        param.put("zy1","1");
////        param.put("zy2","1");
////        System.out.println(getParamXml(param));
//
//
//        StringBuilder sb = new StringBuilder();
//        sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
//        sb.append("<ECSRoot>");
//        sb.append("    <Header>");
//        sb.append("        <msgCode>" + "900" + "</msgCode>");
//        sb.append("        <msgId>" + "8f47b3b5-eed8-44b9-bf87-0b80249db182" + "</msgId>");
//        sb.append("        <createTime>" + "20200324161725" + "</createTime>");
//        sb.append("    </Header>");
//        sb.append("    <Body>");
//        sb.append("             <items>");
//        sb.append("                 <Note>" + "2020-03-24 16:14:18" + "</Note>");
//        sb.append("             </items>");
//        sb.append("    </Body>");
//        sb.append("</ECSRoot>");
//        System.out.println(sb);
//    }


}
